<?php  
use Wdl\helper\sendHelper;
use Wdl\helper\webHelper;

defined('IN_IA') or exit('Access Denied');
global $_GPC, $_W;
load()->func('tpl'); 
 $uniacid = webHelper::bindUniacid(); 
 
$openid = $_W['openid'];
$operation = !empty($_GPC['op']) ? $_GPC['op'] : 'sechouse';	
$template_name = sprintf("sendlist/%s",  $operation);

$rd = !empty($_GPC['rd']) ? $_GPC['rd'] : 'index';
$sec = webHelper::fetch_place_data('setting', 'display');
$selects = $this->_forms;
$selects['areas'] =  $sec['areas'];

$citys = webHelper::citys();
$selects['quan'] = webHelper::quans();

$submenus = webHelper::house_type();

/*如果有ids 表示移动到栏目*/
if($operation == 'display'){
    $sk = trim($_GPC['movhtype']);
    if(isset($submenus[$sk]) && 
        isset($_GPC['ids']) && !empty( $_GPC['ids'] )){
         
        $sqladd_tmp = " zhutype='".$submenus[$sk]['zhutype']."'";
        
        if(!empty($sqladd_tmp )){
            $ids = implode(",", $_GPC['ids']);
            pdo_query("update ".tablename('kbwdl_house'). " set ". $sqladd_tmp ." where uniacid='$uniacid' and id in ($ids) ");
        }        
    }
    message('移动成功', $this->createWebUrl('sendlist',['op'=> $sk]), 'success');
      /*移动房源结束*/ 
}elseif($operation == 'delete'){
    $id = intval($_GPC['id']);
    $info = pdo_fetch("select * from ".tablename('kbwdl_house')." where id='$id' and uniacid='$uniacid'");
    if($info){
        
        /**重新统计房源数量 */
        sendHelper::caculte_house_nums($info,  1);//统计小区
        sendHelper::caculte_house_nums($info,  1, 1); //统计商圈
        /*删除附件图片*/
        pdo_query("delete from " . tablename('kbwdl_house_info') . " where fid='$id'");
        pdo_query("delete from " . tablename('kbwdl_house') . " where id='$id' and uniacid='$uniacid'");
        pdo_query("delete from " . tablename('kbwdl_param') . " where houseid='$id'");

        $gop = webHelper::zhutype_map_type($info['zhutype']);
    }
    message('删除房源成功！', $this->createWebUrl('sendlist', array('op' => !empty($gop ) ? $gop: 'display')), 'success');  
    //设置榜单数据
}elseif($operation =='bangdan'){

    $id = $_GPC['id'];
    $house = pdo_fetch("select * from ".tablename('kbwdl_house')." where id=:id",['id'=>$id]);
   
    if(checksubmit('submit')){
        $ids = $_GPC['ids'];
        $delete = isset($_GPC['delete']) ? $_GPC['delete'] :[];
        $istop = isset($_GPC['istop']) ? $_GPC['istop'] :0 ;
        if($istop){ 
            //取消历史置顶位置
            pdo_query("update ".tablename("kbwdl_house")." set istop=0 where istop=:istop and ishot>0 and uniacid=:uniacid and zhutype=:zhutype",
                    ['uniacid'=> $house['uniacid'], 'zhutype'=> $house['zhutype'],'istop'=>$istop]);
            //设置新置顶位置
           pdo_update("kbwdl_house",['istop'=> $istop], ['id'=> $house['id']]) ; 

        }
        //移除
        if($delete){
            $dids = array_keys($delete);
            pdo_update("kbwdl_house",['istop'=> 0, 'ishot'=>0], ['id'=> $dids]) ; 

        }
        message('设置成功',  $_SERVER['HTTP_REFERER'] , 'success');
    }
  //把现在的都+1；
    pdo_query("update ".tablename("kbwdl_house")." set ishot=ishot+1 where ishot>0 and uniacid=:uniacid and zhutype=:zhutype",
        ['uniacid'=> $house['uniacid'], 'zhutype'=> $house['zhutype']]);
    pdo_update("kbwdl_house",['ishot'=>1], ['id'=> $house['id']]) ;   

    $condition = ' WHERE `uniacid` = :uniacid and ishot>0 and zhutype=:zhutype';
    $params = array(':uniacid' => $uniacid,'zhutype'=> $house['zhutype']);
    /**分shop管理 */
    if(webHelper::bindShopid()!==0){
        $condition.=" AND shopid=:shopid ";
        $params['shopid'] = webHelper::bindShopid();
    }
    $fd = webHelper::table_list_data('kbwdl_house',$condition, $params, "istop desc, ishot asc", 200);
    $list  = $fd['list'];
    //商圈榜单
    $condition.=" and quan=:quan ";
    $params['quan'] = $house['quan'];
    $fd = webHelper::table_list_data('kbwdl_house',$condition, $params, "istop desc, ishot asc", 200);
    $list2  = $fd['list'];
    $condition.=" and village_id=:village_id ";
    $params['village_id'] = $house['village_id'];
    $fd = webHelper::table_list_data('kbwdl_house',$condition, $params, "istop desc, ishot asc", 200);
    $list3  = $fd['list'];
   
    include $this->template('sendlist/bangdan');
    exit;
}elseif($operation=='setproperty'){
    
    $id = intval($_GPC['id']);
    $type = $_GPC['type'];
    $data = intval($_GPC['data']);
    if (in_array($type, array('istop', 'isjingpin', 'ishot', 'show_jiaji','show_you','show_hight'))) {
            $data = ($data==1?'0':'1');
            pdo_update("kbwdl_house", array(  $type => $data), array("id" => $id, "uniacid" => $uniacid));
            die(json_encode(array("result" => 1, "data" => $data)));
    }
    if (in_array($type, array('isonline'))) {
            $data = ($data==1?'0':'1');
            pdo_update("kbwdl_house", array($type => $data), array("id" => $id, "uniacid" => $uniacid));
            die(json_encode(array("result" => 1, "data" => $data)));
    }
    if (in_array($type, array('isdelete'))) {
            $data = ($data==1?'0':'1'); 
            pdo_update("kbwdl_house", array($type => $data), array("id" => $id, "uniacid" => $uniacid));
            die(json_encode(array("result" => 1, "data" => $data)));
    }
    die(json_encode(array("result" => 0)));
}


    $pindex = max(1, intval($_GPC['page']));
    $psize = 15;
    $condition = ' WHERE `uniacid` = :uniacid  ';
    $params = array(':uniacid' => $uniacid);
    if($opersql){
        $condition .= $opersql;
        $params[':uid'] = $_W['uid'];
    }
    if (!empty($_GPC['keyword'])) {
            $condition .= ' AND (`title` LIKE :title OR `village_name` LIKE :title OR `area` LIKE :title OR `quan` LIKE :title OR `house_sno` LIKE :title OR `publish_name`  LIKE :title OR `linkphone` LIKE :title )';
            $params[':title'] = '%' . trim($_GPC['keyword']) . '%';
    }
   if (isset($_GPC['isdelete']) &&  $_GPC['isdelete'] ==1) {
            $condition .= ' AND `isdelete` = :isdelete ';
            $params[':isdelete'] = 1;
    }
    /*查询指定经纪人的*/
     if (isset($_GPC['bid']) && !empty($_GPC['bid'])  ) {
            $condition .= ' AND `broker_id` = :broker_id ';
            $params[':broker_id'] = intval($_GPC['bid']);
    }
    if (isset($_GPC['isonline']) &&  $_GPC['isonline'] ==1) {
            $condition .= ' AND `isonline` = :isonline ';
            $params[':isonline'] = 0;
    }
     
    /*
     *城市区域搜索     * 
     */
    if(isset($_GPC['city_id']) && !empty($_GPC['city_id']) ){
        $condition .= ' AND `city_id` = :city_id';
            $params[':city_id'] = intval($_GPC['city_id']);
            
    }
    if(isset($_GPC['area']) && !empty($_GPC['area']) ){
        $condition .= ' AND `area` = :area';
            $params[':area'] = trim($_GPC['area']);
    }
    /**
     * 按类型搜索
     */
    if(isset($submenus[$operation])){
        $condition.= " AND `zhutype` = :zhutype ";
            $params[':zhutype'] = $submenus[$operation]['zhutype'];
    }

    /**分shop管理 */
    if(webHelper::bindShopid()!==0){
        $condition.=" AND shopid=:shopid ";
        $params['shopid'] = webHelper::bindShopid();
    }
     
    $sql = 'SELECT COUNT(*) FROM ' . tablename('kbwdl_house') . $condition;
    $total = pdo_fetchcolumn($sql, $params);
    if (!empty($total)) {
            $sql = 'SELECT * FROM ' . tablename('kbwdl_house') . $condition . ' ORDER BY `id` DESC, `endtime` DESC,
                            `id` DESC LIMIT ' . ($pindex - 1) * $psize . ',' . $psize;
            $list = pdo_fetchall($sql, $params);
            $pager = pagination($total, $pindex, $psize);
    }
    //移动房源的类型下下拉选项
    $htype_option = [ ];
    foreach($submenus as $k => $m){
        $htype_option[$k] = $m['name'];
    }
include $this->template('sendlist/index');